/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidorped;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import servidorped.NetworkManager1.NetworkReader;
import servidorped.NetworkManager1.NetworkSender;

/**
 * Note: This class is a bit messy.
 * It actually represents two different things in the grand scheme - one is an User as the database understands it
 * and the other is an User as the chat understands it. So the database doesn't know about sockets or messages or conversations,
 * but since the chat does, it just fills in the gaps in the transition.
 */
public class User {

    Socket userSocket;
    NetworkReader userListener;
    NetworkSender userTeller;
    String userName;
    String realName;
    String password;
    String emailAdress;    
    Boolean isAdmin = false;
    Integer conversationID = null;
    private boolean isClosed = false;
    public Boolean isConnected = true;

    public User(Socket userSocket, String userName, String realName, String password, String emailAdress, String isAdmin) {
        this.userSocket = userSocket;
        userListener = new NetworkReader(userSocket);
        try {
            userTeller = new NetworkSender(userSocket);
        } catch (IOException ex) {
            Logger.getGlobal().log(Level.SEVERE, "Socket for user " +userName +" cannot be reached.", ex);
        }
        this.userName = userName;
        this.realName = realName;
        this.password = password;
        this.emailAdress = emailAdress;
        this.isAdmin = Boolean.parseBoolean(isAdmin);
    }

    public User(String userName, String realName, String password, String emailAdress, String isAdmin) {        
        this.userName = userName;
        this.realName = realName;
        this.password = password;
        this.emailAdress = emailAdress;
        this.isAdmin = Boolean.parseBoolean(isAdmin);
    }
    
    public User(String userName, String realName, String password, String emailAdress, boolean isAdmin) {        
        this.userName = userName;
        this.realName = realName;
        this.password = password;
        this.emailAdress = emailAdress;
        this.isAdmin = isAdmin;
    }

    public void loginAs(User identifiedUser) {
        this.emailAdress = identifiedUser.emailAdress;
        this.userName = identifiedUser.userName;
        this.password = identifiedUser.userName;
        this.realName = identifiedUser.realName;
        this.isAdmin = identifiedUser.isAdmin;
    }

    public NetworkReader getUserListener() {
        return userListener;
    }

    public NetworkSender getUserSender() {
        return userTeller;
    }

    public String getEmailAdress() {
        return emailAdress;
    }

    public String getPassword() {
        return password;
    }

    public String getRealName() {
        return realName;
    }

    public Socket getUserSocket() {
        return userSocket;
    }

    public String getUserName() {
        return userName;
    }

    public Integer getConversationID() {
        return conversationID;
    }

    public void setConversation(Conversation convo) {
        if (convo == null) {
            this.conversationID = null;
        } else {
            this.conversationID = convo.getID();
        }
    }

    public boolean isAdmin() {
        return (this.isAdmin);
    }

    public void closeConnection() {
        isConnected = false;
        try {
            if (!userSocket.isClosed())
            this.userSocket.close();
        } catch (IOException ex) {            
        }        
    }
}
